package com.ztgf.shopping.service.template;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.db.utils.RedisUtil;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.template.TempletInfoMapper;
import com.ztgf.shopping.mapper.template.TempletPlatformRelationMapper;
import com.ztgf.shopping.models.bo.template.TempletPlatformRelationSaveBO;
import com.ztgf.shopping.models.dto.template.TempletInfoDTO;
import com.ztgf.shopping.models.dto.template.TempletModuleDTO;
import com.ztgf.shopping.models.dto.template.TempletPlatformRelationDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.template.ITempletPlatformRelationService;
import com.ztgf.shopping.models.vo.template.TemplateInfoDVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 店铺模板关系  服务实现类
 * </p>
 *
 * @author YUE
 * @since 2020-05-23
 */
@DubboService
public class TempletPlatformRelationServiceImpl extends ServiceImpl<TempletPlatformRelationMapper, TempletPlatformRelationDTO> implements ITempletPlatformRelationService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private TempletPlatformRelationMapper templetPlatformRelationMapper;

    @Autowired
    private TempletInfoMapper templetInfoMapper;

    @Autowired
    private TempletModuleServiceImpl templetModuleService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TempletInfoServiceImpl templetInfoService;

    /**
     * 获取平台活动页默认模板id
     * @return
     */
    public Long getDefaultPlateActiveTemplateId(int type){
        Wrapper<TempletPlatformRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("shop_id", 0);
        wrapper.eq("is_default", ShoppingCodeEnum.IS_DEFAULT_TRUE.getCode());
        if(type == ShoppingCodeEnum.TEMPLATE_TYPE_HOME.getCode()){//首页
            wrapper.eq("parent_template_id", 0);
        }else if(type == ShoppingCodeEnum.TEMPLATE_TYPE_ACTIVE.getCode()){//活动页
            wrapper.ne("parent_template_id", 0);
        }
        return selectOne(wrapper).getTempletId();
    }

    @Override
    public boolean add(TempletPlatformRelationSaveBO templetPlatformRelationSaveBO) {
        TempletPlatformRelationDTO templetPlatformRelationDTO = new TempletPlatformRelationDTO();
        BeanUtils.copyProperties(templetPlatformRelationSaveBO,templetPlatformRelationDTO);
        Long id = ymlConfig.getPkId();
        templetPlatformRelationDTO.setId(id);
        return this.insert(templetPlatformRelationDTO);
    }

    /**
     * 根据店铺id获取店铺拥有的默认模板
     * @param shopId
     * @return
     */
    @Override
    public TempletInfoDTO getDefaultTempletInfoListByShopId(Long shopId) {
        TempletPlatformRelationDTO templetPlatformRelationDTO = new TempletPlatformRelationDTO();
        templetPlatformRelationDTO.setShopId(shopId);
        templetPlatformRelationDTO.setIsDefault(0);
        templetPlatformRelationDTO = templetPlatformRelationMapper.selectOne(templetPlatformRelationDTO);
        return templetInfoMapper.selectById(templetPlatformRelationDTO.getTempletId());
    }

    /**
     * 根据店铺id获取店铺拥有的模板
     * @param shopId
     * @param type 0首页 1活动页 2店铺banner模板
     * @return
     */
    @Override
    public List<TemplateInfoDVO> getTempletInfoListByShopId(Long shopId,int type) {
        List<TemplateInfoDVO> templateInfoDVOList = new ArrayList<>();
        List<TempletPlatformRelationDTO> templetPlatformRelationDTOList = getByShopIdAngTyep(shopId, type,0L);
        for (TempletPlatformRelationDTO templetPlatformRelationDTO : templetPlatformRelationDTOList) {
            TempletInfoDTO templetInfoDTO = templetInfoMapper.selectById(templetPlatformRelationDTO.getTempletId());
            TemplateInfoDVO templateInfoDVO = new TemplateInfoDVO();
            BeanUtils.copyProperties(templetInfoDTO,templateInfoDVO);
            templateInfoDVO.setIsDefault(templetPlatformRelationDTO.getIsDefault());
            //templateInfoDVO
            templateInfoDVOList.add(templateInfoDVO);
        }
        return templateInfoDVOList;
    }

    /**
     * 更新默认的模板
     * @param shopId
     * @param templateId
     * @param type
     * @return
     */
    @Transactional
    @Override
    public boolean updateTemplateDefault(Long shopId, Long templateId, int type) {
        List<TempletPlatformRelationDTO> templetPlatformRelationDTOList = getByShopIdAngTyep(shopId, type,0L);
        for (TempletPlatformRelationDTO templetPlatformRelationDTO : templetPlatformRelationDTOList) {
            if(templetPlatformRelationDTO.getTempletId().equals(templateId)){
                templetPlatformRelationDTO.setIsDefault(ShoppingCodeEnum.IS_DEFAULT_TRUE.getCode());
            }else{
                templetPlatformRelationDTO.setIsDefault(ShoppingCodeEnum.IS_DEFAULT_FALSE.getCode());
            }
            boolean b = updateById(templetPlatformRelationDTO);
            if(!b){
                throw new RuntimeException("更新失败,请稍后再试！");
            }
        }
        return true;
    }

    @Override
    public boolean updateActiveTemplateDefault(Long templateId) {
        TempletPlatformRelationDTO tpdto = getByTemplateId(templateId);
        Long parentTemplateId = tpdto.getParentTemplateId();
        List<TempletPlatformRelationDTO> templetPlatformRelationDTOList = getByShopIdAngTyep(0L, 1,parentTemplateId);
        for (TempletPlatformRelationDTO templetPlatformRelationDTO : templetPlatformRelationDTOList) {
//            if(templetPlatformRelationDTO.getIsDefault() == ShoppingCodeEnum.IS_DEFAULT_TRUE.getCode()){
//                templetPlatformRelationDTO.setIsDefault(ShoppingCodeEnum.IS_DEFAULT_FALSE.getCode());
//                updateById(templetPlatformRelationDTO);//修改为不是默认
//                break;
//            }

            if(templetPlatformRelationDTO.getTempletId().equals(templateId)){
                templetPlatformRelationDTO.setIsDefault(ShoppingCodeEnum.IS_DEFAULT_TRUE.getCode());
            } else {
                templetPlatformRelationDTO.setIsDefault(ShoppingCodeEnum.IS_DEFAULT_FALSE.getCode());
            }
            updateById(templetPlatformRelationDTO);
        }
        tpdto.setIsDefault(ShoppingCodeEnum.IS_DEFAULT_TRUE.getCode());//更新默认
        List<TempletModuleDTO> listTemplateModule = templetInfoService.getListTemplateModuleById(templateId);
        redisUtil.savePlateActiveInfo(listTemplateModule);//更新进redis
        return updateById(tpdto);
    }

    /**
     *
     * @param shopId 店铺id
     * @param type 0首页 1活动页 2商品详情页banner
     * @return
     */
    public List<TempletPlatformRelationDTO> getByShopIdAngTyep(Long shopId,int type,Long parentTemplateId){
        Wrapper<TempletPlatformRelationDTO> wrapper = new EntityWrapper();
        wrapper.eq("shop_id", shopId);
        wrapper.eq("type", type);
        wrapper.eq("parent_template_id", parentTemplateId);
        return selectList(wrapper);
    }

    /**
     * 根据父模板id获取所属模板
     * @param parentTemplateId 父模板id
     * @return
     */
    public List<TempletPlatformRelationDTO> getByParentId(Long parentTemplateId){
        Wrapper<TempletPlatformRelationDTO> wrapper = new EntityWrapper();
        wrapper.eq("parent_template_id", parentTemplateId);
        return selectList(wrapper);
    }

    /**
     * 根据模板id获取所属模板关联关系信息
     * @param templateId 模板id
     * @return
     */
    public TempletPlatformRelationDTO getByTemplateId(Long templateId){
        Wrapper<TempletPlatformRelationDTO> wrapper = new EntityWrapper();
        wrapper.eq("templet_id", templateId);
        return selectOne(wrapper);
    }


    /**
     * 根据模板id删除关联信息
     * @param templateId 模板id
     * @return
     */
    public boolean deleteByTemplateId(Long templateId){
        Wrapper<TempletPlatformRelationDTO> wrapper = new EntityWrapper();
        wrapper.eq("templet_id", templateId);
        return delete(wrapper);
    }
}
